package com.sg.business.commons.field;

import java.util.ArrayList;
import java.util.Arrays;

import org.eclipse.core.runtime.ListenerList;
import org.eclipse.jface.fieldassist.ContentProposal;
import org.eclipse.jface.fieldassist.ContentProposalAdapter;
import org.eclipse.jface.fieldassist.IContentProposal;
import org.eclipse.jface.fieldassist.IContentProposalProvider;
import org.eclipse.jface.fieldassist.TextContentAdapter;
import org.eclipse.rap.addons.autosuggest.AutoSuggest;
import org.eclipse.rap.addons.autosuggest.ColumnDataProvider;
import org.eclipse.rap.addons.autosuggest.ColumnTemplate;
import org.eclipse.rap.addons.autosuggest.DataSource;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;

import com.mobnut.commons.util.Utils;
import com.sg.business.model.User;

public class UserInputText extends Text implements IContentProposalProvider {

	private ListenerList listners;
	private User[] dataSet;

	public UserInputText(Composite parent, int style) {
		super(parent, style);
	}

	public void setupDataSource(User[] dataSet) {
		this.dataSet = dataSet;
		if (Utils.isIE8Client()) {
			initControlIE8();
		} else {
			initControl();
		}
	}

	private void initControl() {
		AutoSuggest autoSuggest = new AutoSuggest(this);
		autoSuggest.setDataSource(getDataSource());
	}

	private void initControlIE8() {
		ContentProposalAdapter cpa = new ContentProposalAdapter(this,
				new TextContentAdapter(), this, null, null);
		cpa.setProposalAcceptanceStyle(ContentProposalAdapter.PROPOSAL_REPLACE);
	}

	public void addValueChangedListener(IValueChangedListener listener) {
		if (listners == null) {
			listners = new ListenerList();
		}
		listners.add(listener);
	}

	public void removeValueChangedListener(IValueChangedListener listener) {
		if (listners != null) {
			listners.remove(listener);
		}
	}

	@Override
	public IContentProposal[] getProposals(String contents, int position) {
		ArrayList<ContentProposal> result = new ArrayList<ContentProposal>();
		for (int i = 0; i < dataSet.length; i++) {
			User user = dataSet[i];
			String userid = user.getUserid();
			String userName = user.getUsername();
			String input = contents.toLowerCase();

			if (userid.startsWith(input)
					|| userName.contains(input)
					|| Utils.getAlphaString(userName).toLowerCase()
							.startsWith(input)) {
				ContentProposal cp = new ContentProposal(user.getLabel());
				result.add(cp);
			}
		}
		return result.toArray(new IContentProposal[0]);
	}

	private DataSource getDataSource() {
		DataSource dataSource = new DataSource();
		dataSource.setTemplate(new ColumnTemplate(120, 200)); // the column
																// widths
		final String[][] suggestions = new String[dataSet.length][2];
		for (int i = 0; i < suggestions.length; i++) {
			suggestions[i][0] = dataSet[i].getUserid();
			suggestions[i][1] = dataSet[i].getLabel();
		}

		ColumnDataProvider dataProvider = new ColumnDataProvider() {
			public Iterable<?> getSuggestions() {
				return Arrays.asList(suggestions);
			}

			public String getValue(Object element) {
				String[] value = (String[]) element;
				return value[0];
			}

			public String[] getTexts(Object element) {
				String[] value = (String[]) element;
				return new String[] { value[0], value[1] };
			}
		};
		dataSource.setDataProvider(dataProvider);
		return dataSource;
	}

}
